layui.use(['form', 'layedit', 'laydate', 'jquery'], function() {
    var form = layui.form
        , layer = layui.layer
        , layedit = layui.layedit
        , laydate = layui.laydate
        , $ = layui.$;

// 生成uuid：
    function generateUUID() {
        let d = new Date().getTime(); // 获取当前时间
        let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
            let r = (d + Math.random() * 16) % 16 | 0;
            d = Math.floor(d / 16);
            return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
        });
        return uuid;
    }
    // uuid = generateUUID();
    // console.log(generateUUID());
    // 获取图片
    function get_imgverify() {
      uuid = generateUUID();
      $('#img_verify').attr('src', '/user/imageverify/' + uuid + '/')
    }

    get_imgverify();

    //实现点击刷新
    $('#img_verify').click(get_imgverify);

    //三分钟自动刷新图片
    setInterval(get_imgverify, 3 * 60 * 1000);

   //自定义验证规则
  form.verify({
    pass: [
      /^[\S]{6,12}$/
      ,'密码必须6到12位，且不能出现空格'
    ]
    ,verify:[
      /^[\S]{4}$/
      ,'验证码必须4位'
    ]
  });

//监听提交
  form.on('submit(login)', function(data){
    // layer.alert(JSON.stringify(data.field), {
    //   title: '最终的提交信息'
    // })
    $.post('/user/loginverify/' + uuid + '/', data.field, function (data) {
      if (data.code == 4001 || data.code == 4004 || data.code == 4005){
        layer.msg(data.msg, {icon: 5});
        return false;
      }
      if (data.code == 0){
        localStorage.setItem('Authorization', data.data);
        location.href = '/static/index.html';
      }
    });
    return false;
  });
});
